package com.retail.booking.client.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.retail.booking.client.BookingPosterClient;
import com.retail.booking.entity.request.BookingSharePosterConfigRequest;
import com.retail.booking.entity.request.BookingShareRecordRequest;
import com.retail.booking.entity.request.BookingUserCreateShareRequest;
import com.retail.booking.entity.request.BookingXiaohesuanRequest;
import com.retail.booking.entity.vo.BookingSharePosterConfigVo;
import com.retail.booking.service.BookingSharePosterConfigService;
import com.retail.booking.service.BookingShareRecordService;
import com.retail.booking.service.BookingUserCreateShareService;
import com.retail.booking.service.BookingXiaohesuanService;
import com.retail.core.entity.ParamReq;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service("bookingPosterClient")
public class BookingPosterDefaultClientImpl implements BookingPosterClient {

    private final BookingSharePosterConfigService bookingSharePosterConfigService;

    private final BookingShareRecordService bookingShareRecordService;

    private final BookingUserCreateShareService bookingUserCreateShareService;

    private final BookingXiaohesuanService bookingXiaohesuanService;

    public BookingPosterDefaultClientImpl(BookingSharePosterConfigService bookingSharePosterConfigService, BookingShareRecordService bookingShareRecordService, BookingUserCreateShareService bookingUserCreateShareService, BookingXiaohesuanService bookingXiaohesuanService) {
        this.bookingSharePosterConfigService = bookingSharePosterConfigService;
        this.bookingShareRecordService = bookingShareRecordService;
        this.bookingUserCreateShareService = bookingUserCreateShareService;
        this.bookingXiaohesuanService = bookingXiaohesuanService;
    }

    @Override
    public Result<BookingSharePosterConfigVo> getPoster() {
        BookingSharePosterConfigRequest bookingSharePosterConfigRequest = new BookingSharePosterConfigRequest();
        bookingSharePosterConfigRequest.setDelFlag(DelFlagEnum.NO.getValue());
        bookingSharePosterConfigRequest.setStatusFlag(StatusFlagEnum.USE.getValue());

        ParamReq<BookingSharePosterConfigRequest> paramReq = new ParamReq<>();
        paramReq.setCurrent(1);
        paramReq.setSize(10);
        paramReq.setEntity(bookingSharePosterConfigRequest);
        List<BookingSharePosterConfigVo> posters = bookingSharePosterConfigService.listPage(paramReq);
        if (ObjectUtil.isNotEmpty(posters) && posters.size() > 0) {
            return Result.success(posters.get(0));
        }
        return Result.fail();
    }

    @Override
    public Result createBookingShareRecord(BookingUserCreateShareRequest request) {
        try {
            request.setDelFlag(DelFlagEnum.NO.getValue());
            request.setStatusFlag(StatusFlagEnum.USE.getValue());
            Long id = bookingUserCreateShareService.saveRequestReturnId(request);
            return Result.success("生成记录成功！", id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @Override
    public Result saveShareRecord(BookingShareRecordRequest request) {
        try {
            request.setBookingTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            request.setDelFlag(DelFlagEnum.NO.getValue());
            request.setStatusFlag(StatusFlagEnum.USE.getValue());
            bookingShareRecordService.save(request);
            return Result.success("分享成功！");
        } catch (Exception e) {
            return Result.fail("分享失败！");
        }
    }

    @Override
    public Result saveXiaohesuan(BookingXiaohesuanRequest request) {
        try {
            request.setDelFlag(DelFlagEnum.NO.getValue());
            request.setStatusFlag(StatusFlagEnum.USE.getValue());
            bookingXiaohesuanService.save(request);
            return Result.success("预约小核算成功！");
        } catch (Exception e) {
            return Result.fail("预约小核算失败！");
        }
    }
}
